﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="GestureEngineSettingsWindow.cs" company="arvystate.net">
//   arvystate.net
// </copyright>
// <summary>
//   Class acts as a wrapper for Engine settings
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace PresentationAid.Lib.GestureEngines.GestureSettings
{
    using System;
    using System.ComponentModel;
    using System.Windows;

    using NLog;

    using PresentationAid.Settings;

    using Window = Elysium.Controls.Window;

    /// <summary>
    /// Class acts as a wrapper for Engine settings
    /// </summary>
    public abstract class GestureEngineSettingsWindow : Window
    {
        #region Static Fields

        /// <summary>
        /// The logger.
        /// </summary>
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="GestureEngineSettingsWindow"/> class.
        /// </summary>
        protected GestureEngineSettingsWindow()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="GestureEngineSettingsWindow"/> class. 
        /// Constructor ensures functionality of the engine settings window.
        /// </summary>
        /// <param name="engine">
        /// Gesture engine parent of the settings window
        /// </param>
        protected GestureEngineSettingsWindow(GestureEngine engine)
        {
            Logger.Trace("Init: GestureEngineSettingsWindow");

            this.Engine = engine;

            try
            {
                this.Owner = Application.Current.MainWindow;
                this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            }
            catch (Exception)
            {
                this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            }

            this.ResizeMode = ResizeMode.NoResize;

            this.Width = 640;
            this.Height = 480;

            this.Closing += this.OnClosing;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the engine.
        /// </summary>
        protected GestureEngine Engine { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The create settings.
        /// </summary>
        public abstract void CreateSettings();

        /// <summary>
        /// The load settings.
        /// </summary>
        public abstract void LoadSettings();

        #endregion

        #region Methods

        /// <summary>
        /// The restore defaults button click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        protected void RestoreDefaultsButtonClick(object sender, RoutedEventArgs e)
        {
            this.Engine.SetDefaultMapping();
            this.LoadSettings();

            SettingManager.Instance.GestureEngineMapping = this.Engine.GetMapping();
        }

        /// <summary>
        /// The save button click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        protected void SaveButtonClick(object sender, RoutedEventArgs e)
        {
            SettingManager.Instance.GestureEngineMapping = this.Engine.GetMapping();
        }

        /// <summary>
        /// The on closing.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="cancelEventArgs">
        /// The cancel event args.
        /// </param>
        private void OnClosing(object sender, CancelEventArgs cancelEventArgs)
        {
            cancelEventArgs.Cancel = true;

            this.Hide();
        }

        #endregion
    }
}